<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\controllers;

/**
 * The <b>StubController</b> class is a <i>Controller</i> which populates it's context
 * (MVC <i>Model</i> part) with <i>Stub</i> data.
 * 
 * <p>The <b>StubController</b> can serve different purposes like :
 * <ul>
 * 	<li>filling the context with static data without hardcoding it;</li>
 * 	<li>providing in-place controllers in early developments, for instance during design phases.</li>
 * </ul>
 * </p>
 * 
 * <p>If needed, the <b>StubController</b> can be extended to have a hybrid controller with both static
 * and dynamic data to fill the context with. You might also consider using a ResourceBundle from the
 * InternationalizationHelper for static data.</p>
 * 
 * <p>XML declaration :
 * <code>
 * <controller name="myStub" type="switcher\controllers\StubController" default-action="loadJson">
 *     <stub>includes/views/myStub.json</stub>
 * </controller>
 * </code>
 * 
 * It is also possible to declare a stub directly with data, but it's not recommended :
 * <code>
 * <controller name="myStub" type="switcher\controllers\StubController" default-action="loadJson">
 *     <stub>{ "myData" : "myValue" }</stub>
 * </controller>
 * </code>
 * </p>
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @package switcher\controllers
 * @since 1.0.0
 * @see switcher\controllers\Controller
 * @see switcher\utils\JsonHelper
 */
class StubController extends \switcher\controllers\Controller
{
	/**
	 * <p>The stub, can be either a file name or raw content.
	 * Populated by the Switcher framework or a subclass.</p>
	 * @var string
	 */
	protected $stub = '';
	
	/**
	 * Populates the context with <i>JSON</i> data.
	 * 
	 * @throws \InvalidArgumentException If no JSON data could be found from the stub
	 * @see switcher\utils\JsonHelper
	 */
	public function loadJson()
	{
		if ( preg_match('/^\s*({\s*}\s*)?$/', $this->stub) )
		{
			return;
		}
		
		$values = json_decode($this->stub);
		
		if ($values == null)
		{
			$values = \switcher\utils\JsonHelper::loadJson($this->stub);
		}
		
		if ($values == null)
		{
			throw new \InvalidArgumentException('Invalid stub : ' . $this->stub);
		}
		
		$this->setContextParameters((array) $values);
	}
}

?>